package cn.exrick.front.controller;

import cn.exrick.common.constant.CommonConstant;
import cn.exrick.common.pojo.Result;
import cn.exrick.common.utils.ResultUtil;
import cn.exrick.manager.dto.front.Cart;
import cn.exrick.manager.dto.front.CartProduct;
import cn.exrick.manager.pojo.TbMember;
import cn.exrick.sso.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@Controller
@Api(description = "购物车")
public class CartController {

    @Autowired
    private CartService cartService;

    @ResponseBody
    @RequestMapping(value = "/cart/addCart",method = RequestMethod.POST)
    @ApiOperation(value = "添加购物车商品")
    public Result<Object> addCart(Cart cart,HttpSession session){
        TbMember member = (TbMember) session.getAttribute(CommonConstant.USER_SESSION);
        int result=cartService.addCart(member.getId(),cart.getProductId(),cart.getProductNum());
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/cart")
    public String toCart(){
        return "shopping-cart";
    }

    @RequestMapping(value = "/cart/list")
    @ApiOperation(value = "获取购物车商品列表")
    public String listCart(Model model, HttpSession session){
        TbMember member = (TbMember) session.getAttribute(CommonConstant.USER_SESSION);
        if(member != null){
            List<CartProduct> cartProductList=cartService.getCartList(member.getId());
            BigDecimal total = new BigDecimal(0);
            for(CartProduct cartProduct : cartProductList){
                BigDecimal subTotal = cartProduct.getSalePrice().multiply(new BigDecimal(cartProduct.getProductNum()));
                subTotal.setScale(2, BigDecimal.ROUND_HALF_UP);
                cartProduct.setSubTotal(subTotal);
                total = total.add(subTotal);
            }
            model.addAttribute("cartProductList",cartProductList);
            model.addAttribute("total",total);
            model.addAttribute("member",member);
        }

        return "shopping-cart-detail";
    }

    @ResponseBody
    @RequestMapping(value = "/cart/cartEdit",method = RequestMethod.POST)
    @ApiOperation(value = "编辑购物车商品")
    public Result<Object> updateCartNum(Cart cart,HttpSession session){
        TbMember member = (TbMember) session.getAttribute(CommonConstant.USER_SESSION);
        int result=cartService.updateCartNum(member.getId(),cart.getProductId(),cart.getProductNum(),cart.getChecked());
        return new ResultUtil<Object>().setData(result);
    }

    @ResponseBody
    @RequestMapping(value = "/cart/editCheckAll",method = RequestMethod.POST)
    @ApiOperation(value = "是否全选购物车商品")
    public Result<Object> editCheckAll(Cart cart){

        int result=cartService.checkAll(cart.getUserId(),cart.getChecked());
        return new ResultUtil<Object>().setData(result);
    }

    @ResponseBody
    @RequestMapping(value = "/cart/cartDel",method = RequestMethod.POST)
    @ApiOperation(value = "删除购物车商品")
    public Result<Object> deleteCartItem(Long productId,HttpSession session){
        TbMember member = (TbMember) session.getAttribute(CommonConstant.USER_SESSION);
        int result=cartService.deleteCartItem(member.getId(),productId);
        return new ResultUtil<Object>().setData(result);
    }

    @ResponseBody
    @RequestMapping(value = "/cart/delCartChecked",method = RequestMethod.POST)
    @ApiOperation(value = "删除购物车选中商品")
    public Result<Object> delChecked(Cart cart){

        cartService.delChecked(cart.getUserId());
        return new ResultUtil<Object>().setData(null);
    }
}
